﻿<%@ Page Title="نقش عضو" Language="C#" MasterPageFile="~/App_MasterPages/DefaultMasterPage.master"
    AutoEventWireup="true" CodeFile="UserRole.aspx.cs" Inherits="UserRole" %>

<%@ Register Assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="UserContent" ContentPlaceHolderID="DefaultContentPlaceHolder"
    runat="Server">
      <script language="javascript" type="text/javascript">
        function Mandatory(sender, e) {
            e.IsValid = (e.Value != '0');
        }        
    </script>
    <ajaxControlToolkit:ToolkitScriptManager runat="Server" ID="ScriptManager1" />
    <asp:MultiView ID="MultiView" runat="server" ActiveViewIndex="0">
        <asp:View ID="ShowView" runat="server">
            <table style="width: 480px" title="کلاس">
                <tr dir="rtl">
                    <td style="width: 120px; text-align: right;">
                        نام :
                    </td>
                    <td style="width: 100px; text-align: right;">
                        <asp:TextBox ID="FirstNameTextBox" runat="server" Width="100px"></asp:TextBox>
                    </td>
                    <td style="width: 120px" align="right">
                        نام خانوادگی :
                    </td>
                    <td style="width: 110px">
                        <asp:TextBox ID="LastNameTextBox" runat="server" Width="100px"></asp:TextBox>
                    </td>
                    <td style="width: 30px">
                        <asp:ImageButton ID="ClearImageButton" runat="server" OnClick="ClearImageButton_Click"
                            OnClientClick="return false;" SkinID="ClearButton" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 120px; text-align: right;">
                        شماره عضویت :
                    </td>
                    <td style="width: 100px; text-align: right;">
                        <asp:TextBox ID="UserNoTextBox" runat="server" Width="100px"></asp:TextBox>
                    </td>
                    <td align="right" style="width: 120px">
                        سایز صفحه :
                    </td>
                    <td style="width: 110px" align="right">
                        <asp:DropDownList ID="PageSizeDropDownList" SkinID="SearchDropDownList" runat="server">
                            <asp:ListItem Selected="True" Value="5">[پیش فرض]</asp:ListItem>
                            <asp:ListItem Value="5">5 رکورد</asp:ListItem>
                            <asp:ListItem Value="10">10 رکورد</asp:ListItem>
                            <asp:ListItem Value="15">15 رکورد</asp:ListItem>
                            <asp:ListItem Value="20">20 رکورد</asp:ListItem>
                            <asp:ListItem Value="25">25 رکورد</asp:ListItem>
                            <asp:ListItem Value="1000000">تمام رکورد ها</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td style="width: 30px">
                        <asp:ImageButton ID="SearchImageButton" runat="server" OnClick="SearchImageButton_Click"
                            SkinID="SearchButton" />
                    </td>
                </tr>
                <tr>
                    <td colspan="5">
                        <asp:GridView ID="UserGridView" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                            DataKeyNames="UserID" DataSourceID="UserEntityDataSource" HorizontalAlign="Center"
                            OnSelectedIndexChanged="UserGridView_SelectedIndexChanged" Width="100%" AllowSorting="false">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:ImageButton ID="SelectImageButton" runat="server" CommandName="Select" SkinID="SelectButton" /></ItemTemplate>
                                    <ItemStyle Width="25px" />
                                </asp:TemplateField>
                                <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True"
                                    Visible="false" SortExpression="UserID" />
                                <asp:BoundField DataField="UserName" HeaderText="شماره عضویت" SortExpression="UserName">
                                    <HeaderStyle Font-Size="Smaller" />
                                    <ItemStyle Width="100px" />
                                </asp:BoundField>
                                <asp:BoundField DataField="FirstName" HeaderText="نام" SortExpression="FirstName">
                                </asp:BoundField>
                                <asp:BoundField DataField="LastName" HeaderText="نام خانوادگی" SortExpression="LastName" />
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </asp:View>
        <asp:View ID="EditView" runat="server">
            <table>
                <tr>
                    <td>
                        <asp:Label ID="UserLabel" runat="server" Text=""></asp:Label>
                    </td>
                    <td>
                    </td>
                    <td>
                        <asp:LinkButton ID="ReturnLinkButton" runat="server" OnClick="ReturnLinkButton_Click">بازگشت</asp:LinkButton>
                    </td>
                </tr>
                <tr>
                    <td valign="top">
                        <asp:GridView ID="UserRoleGridView" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                            DataKeyNames="UserID,RoleID" DataSourceID="UserRoleEntityDataSource"
                            HorizontalAlign="Center" OnSelectedIndexChanged="UserRoleGridView_SelectedIndexChanged"
                            Width="220px" AllowSorting="True" PageSize="5">
                            <Columns>
                                <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True"
                                    SortExpression="UserID" Visible="false" />
                                <asp:BoundField DataField="RoleID" HeaderText="RoleID" ReadOnly="True"
                                    SortExpression="RoleID" Visible="false" />
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:ImageButton ID="SelectImageButton" runat="server" CommandName="Select" SkinID="SelectButton" /></ItemTemplate>
                                    <ItemStyle Width="25px" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="نقش" SortExpression="RoleID">
                                    <ItemTemplate>
                                        <asp:DropDownList ID="RoleDropDownList" runat="server" Enabled="False" SkinID="GridDropDownList"
                                            DataSourceID="RoleEntityDataSource" DataTextField="Name" DataValueField="ID"
                                            SelectedValue='<%# Bind("RoleID") %>'>
                                        </asp:DropDownList>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                    <td style="width: 20px">
                    </td>
                    <td>
                        <asp:FormView ID="UserRoleFormView" runat="server" DataSourceID="UserRoleEntityDataSource"
                            DataKeyNames="UserID,RoleID" DefaultMode="Insert" OnItemCommand="UserRoleFormView_ItemCommand"
                            Width="240px">
                            <EditItemTemplate>
                                <table border="0" cellpadding="0" cellspacing="0" style="width: 240px">
                                    <tr>
                                        <td style="width: 80px" align="right">
                                            نقش :
                                        </td>
                                        <td style="width: 160px" align="right">
                                            <asp:DropDownList ID="RoleDropDownList" Enabled="false" runat="server" DataSourceID="RoleEntityDataSource"
                                                DataTextField="Name" DataValueField="ID" SkinID="FormDropDownList" SelectedValue='<%# Bind("RoleID") %>'>
                                            </asp:DropDownList>
                                            <asp:CustomValidator ID="RoleCustomValidator" runat="server" ClientValidationFunction="Mandatory"
                                                ControlToValidate="RoleDropDownList" Display="Dynamic" ErrorMessage="*" ValidateEmptyText="True"
                                                ValidationGroup="Update"></asp:CustomValidator>
                                            <asp:HiddenField ID="UserIDHiddenField" runat="server" Value='<%# Bind("UserID") %>' />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" align="left">
                                            <table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
                                                <tr>
                                                    <td style="height: 15px">
                                                    </td>
                                                    <td align="left" valign="top" style="width: 80px;">
                                                    </td>                                              
                                                    <td align="left" valign="top" style="width: 80px;">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="height: 15px">
                                                        &nbsp;
                                                    </td>
                                                    <td align="left" valign="top" style="width: 80px">
                                                        <asp:ImageButton ID="DeleteImageButton" runat="server" CausesValidation="False" CommandName="Delete"
                                                            SkinID="DeleteButton" OnClientClick="if (!confirm('آيا از حذف مطمئن هستيد؟')) return false;" />
                                                    </td>                                                   
                                                    <td align="left" valign="top" style="width: 80px">
                                                        <asp:ImageButton ID="CancelImageButton" runat="server" CommandName="Cancel" SkinID="CancelButton" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </EditItemTemplate>
                            <InsertItemTemplate>
                                <table border="0" cellpadding="0" cellspacing="0" style="width: 240px">
                                    <tr>
                                        <td style="width: 80px" align="right">
                                            نقش :
                                        </td>
                                        <td style="width: 160px" align="right">
                                            <asp:DropDownList ID="RoleDropDownList" runat="server" DataSourceID="RoleEntityDataSource"
                                                DataTextField="Name" DataValueField="ID" SkinID="FormDropDownList" SelectedValue='<%# Bind("RoleID") %>'>
                                            </asp:DropDownList>
                                            <asp:CustomValidator ID="RoleCustomValidator" runat="server" ClientValidationFunction="Mandatory"
                                                ControlToValidate="RoleDropDownList" Display="Dynamic" ErrorMessage="*" ValidateEmptyText="True"
                                                ValidationGroup="Insert"></asp:CustomValidator>
                                            <asp:HiddenField ID="UserIDHiddenField" runat="server" Value='<%# Bind("UserID") %>' />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" align="left">
                                            <table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
                                                <tr>
                                                    <td style="height: 15px">
                                                        &nbsp;
                                                    </td>
                                                    <td align="left" valign="top" style="width: 80px;">
                                                    </td>
                                                    <td align="left" valign="top" style="width: 80px;">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td style="height: 15px">
                                                        &nbsp;
                                                    </td>
                                                    <td align="left" valign="top" style="width: 80px;">
                                                        <asp:ImageButton ID="InsertButton" runat="server" CommandName="Insert" SkinID="InsertButton"
                                                            ValidationGroup="Insert" OnClick="InsertButton_Click" />
                                                    </td>
                                                    <td align="left" valign="top" style="width: 80px;">
                                                        <asp:ImageButton ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                                            SkinID="CancelButton" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </InsertItemTemplate>
                        </asp:FormView>
                    </td>
                </tr>
            </table>
        </asp:View>
    </asp:MultiView>
    <asp:EntityDataSource ID="UserRoleEntityDataSource" runat="server" ConnectionString="name=DefaultEntityConnection"
        DefaultContainerName="DefaultEntityConnection" EntitySetName="UserRole" Include="Role,User"
        Where="it.UserID= @UserID" EnableDelete="True" 
          EnableInsert="True" EnableUpdate="True" 
          ondeleted="UserRoleEntityDataSource_Command" 
          oninserted="UserRoleEntityDataSource_Command" 
          onupdated="UserRoleEntityDataSource_Command">
        <WhereParameters>
            <asp:ControlParameter ControlID="UserIDHiddenField" DbType="Int64" Name="UserID"
                PropertyName="Value" DefaultValue="0" />
        </WhereParameters>
    </asp:EntityDataSource>
    <asp:EntityDataSource ID="RoleEntityDataSource" runat="server" ConnectionString="name=DefaultEntityConnection"
        DefaultContainerName="DefaultEntityConnection" CommandText="(SELECT  it.[RoleID] As ID, it.[RoleTitle] As Name FROM DefaultEntityConnection.Role AS it) UNION (SELECT x.[Value] as ID,x.[Text] As Name from DefaultEntityConnection.NoSelection as x)">
    </asp:EntityDataSource>
    <asp:EntityDataSource ID="UserEntityDataSource" runat="server" ConnectionString="name=DefaultEntityConnection"
        DefaultContainerName="DefaultEntityConnection" EntitySetName="User" Where="(@FirstName is null || it.FirstName LIKE '%' + @FirstName + '%') and (@LastName is null || it.LastName LIKE '%' + @LastName + '%') and (@UserName is null || it.UserName LIKE '%' + @UserName + '%')"
        Select="it.[UserID], it.[UserName], it.[FirstName], it.[LastName]">
        <WhereParameters>
            <asp:ControlParameter ControlID="FirstNameTextBox" Name="FirstName" Type="String" />
            <asp:ControlParameter ControlID="LastNameTextBox" Name="LastName" Type="String" />
            <asp:ControlParameter ControlID="UserNameTextBox" Name="UserName" Type="String" />
        </WhereParameters>
    </asp:EntityDataSource>
    <asp:HiddenField ID="UserIDHiddenField" runat="server" />
</asp:Content>
